﻿'Copyright 2010 Brian L. Crawford

'Licensed under the Apache License, Version 2.0 (the "License");
'you may not use this file except in compliance with the License.
'You may obtain a copy of the License at

'    http://www.apache.org/licenses/LICENSE-2.0

'Unless required by applicable law or agreed to in writing, software
'distributed under the License is distributed on an "AS IS" BASIS,
'WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'See the License for the specific language governing permissions and
'limitations under the License.

'This software uses Microsoft .Net framework used with permission from      
'Microsoft. Microsoft, .Net Framework, Visual Studio, Visual Basic, Visual C++ 
'remain the trademark and copyright of Microsoft and are subject to their 
'individual licenses.



Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Public Module BrowseFolder
    Function GetBrowse(Optional ByVal pszTitle As String = "Select a Folder", Optional ByVal previouspath As String = Nothing)
        Dim strPath As String
        Dim newFolder As New FolderBrowserDialog
        newFolder.Description() = pszTitle
        newFolder.SelectedPath = previouspath
        newFolder.ShowDialog()
        strPath = newFolder.SelectedPath()
        GetBrowse = strPath
    End Function

    Function GetBrowseFiles(Optional ByVal Title As String = "Pick a File", Optional ByVal previouspath As String = Nothing, _
                             Optional ByVal AllowMultipleFileSelection As Boolean = False)
        Dim strpath As String = Nothing
        Dim myFile As New OpenFileDialog
        'The calling function can determine wether or not to 
        'allow multiple file selection, default is False
        myFile.Title = Title
        myFile.Multiselect = AllowMultipleFileSelection
        'The calling function can set a fileopen path
        'the default is My Documents if nothing is given
        If previouspath IsNot Nothing Then
            myFile.InitialDirectory = previouspath
        Else
            myFile.InitialDirectory = Environment.SpecialFolder.MyDocuments
        End If
        myFile.Filter = "Excel Files (*.xls;*.xls;*.xlt;*.xlm)|*.xls;*.xls;*.xlt;*.xlm" & _
                            "|CSV Files (*.csv)|*.csv|All Files (*.*)*.*"
        myFile.FilterIndex = 1
        myFile.RestoreDirectory = True
        'Lets get some files  
        'need to determine if we should open the file stream here or pass 
        'the file path back to the orignial calling function.
        'maybe let the calling function decide.
        If myFile.ShowDialog() = DialogResult.OK Then
            Try
                strpath = myFile.FileName

            Catch ex As Exception
                MessageBox.Show("Can't read that file.  Here's why: " & ex.Message)

            End Try

        End If
        If strpath IsNot Nothing Then Return strpath
    End Function


End Module
